package Records.UserInterface;

import Records.Connector.CheckConnection;
import Records.Main.RecordsMain;
import Records.Attributes.SaveFeedData;
import Records.Attributes.SendFeedData;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Alexander
 */
public class SplashClass extends Thread {

    private JLabel jLabel2;
    private static SendFeedData Send;
    private JDialog dialog;
    private JProgressBar progress;
    private int load;
    private int preRssExtractCount;
    
    public SplashClass() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SplashClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        System.out.println("Song_Count.run()");
        boolean proceed = true;
        CheckConnection CC = new CheckConnection("http://www.google.com");
        if (CC.isConnected()) {
            try {
                RecordsMain.dba.truncateChartTables();
            } catch (SQLException ex) {
                proceed = false;
            }
            
            if (proceed == true) {
                try {
                    showSplashScreen();
                } catch (MalformedURLException ex) {
                    Logger.getLogger(SplashClass.class.getName()).log(Level.SEVERE, null, ex);
                }
                Send = new SendFeedData();
                Send.start();
            } else {
                RecordsMain.getUI().setVisible(true);
                hideSplashScreen();
            }
        } else {
            JOptionPane.showMessageDialog(null, "There are problems connecting to the internet. Connect to the internet and restart.", "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
        SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {

            @Override
            protected Void doInBackground() throws Exception {
                while (SaveFeedData.getRssSongCounter() < preRssExtractCount) {
                    load = ((SaveFeedData.getRssSongCounter() * 100) / preRssExtractCount);
                    publish(load);// Notify progress
                    jLabel2.setText(SaveFeedData.getCurSavedSong());
                }
                return null;
            }
            
            @Override
            protected void process(List<Integer> chunks) {
                progress.setValue(load);
            }
            
            @Override
            protected void done() {
                RecordsMain.getUI().setVisible(true);
                hideSplashScreen();
            }
        };
        worker.execute();
    }

    protected void hideSplashScreen() {
        System.out.println("Song_Count.hideSplashScreen()");
        dialog.setVisible(false);
        dialog.dispose();
    }

    protected void showSplashScreen() throws MalformedURLException {
        System.out.println("Song_Count.showSplashScreen()");
        dialog = new JDialog((Frame) null);
        jLabel2 = new javax.swing.JLabel();

        dialog.setModal(false);
        dialog.setUndecorated(true);
        progress = new JProgressBar();
        progress.setStringPainted(true);

        JLabel background = new JLabel();

        background.setIcon(new javax.swing.ImageIcon("Records_Program_Splash.png")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(dialog.getContentPane());
        dialog.getContentPane().setLayout(layout);
        dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        dialog.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        dialog.getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 240, 430, -1));

        progress.setName(""); // NOI18N
        dialog.getContentPane().add(progress, new org.netbeans.lib.awtextra.AbsoluteConstraints(4, 256, 444, -1));

        background.setIcon(new javax.swing.ImageIcon("Records_Program_Splash.png")); // NOI18N
        dialog.getContentPane().add(background, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 275));

        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    public void addToPreRssExtractCount(int preRssExtractCount) {
        this.preRssExtractCount = this.preRssExtractCount+ preRssExtractCount;
    }
}
